package com.curvebreakers.wheresmystuff.model;

import com.curvebreakers.wheresmystuff.application.WMSApplication;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * A singleton that represents the current session of the application and keeps track
 * of the currently logged in user. 
 * @author evan.cahill
 */
public class Session implements DBResultListener {
	
	/** The current session of the application */
	private static final Session currentSession = new Session();
	/** A null user used when no user is logged in. */
	//private static final User nullUser = new User(null, null, null);
	/** The currently logged in user. */
	private User user;
	
	private Session() {
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(WMSApplication.getContext());
		if (settings.contains("user_id")) {
			user = new User(settings.getInt("user_id", 0), settings.getString("email", ""), settings.getString("phone_num", ""), settings.getBoolean("admin", false), settings.getBoolean("locked", false));
			DatabaseModel.getUser(settings.getInt("user_id", 0), this);
		}
	}
	
	/**
	 * Returns the current session.
	 * @return the current session.
	 */
	public static Session getSession() {
		return currentSession;
	}
	
	/**
	 * Logs the specified user in to the session.
	 * @param user
	 */
	public void login(User user) {
		this.user = user;
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(WMSApplication.getContext());
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt("user_id", user.getId());
		editor.putString("email", user.getEmail());
		editor.putString("phone_num", user.getPhoneNum());
		editor.putBoolean("admin", user.isAdmin());
		editor.putBoolean("locked", user.isLocked());
		editor.commit();
	}
	
	/**
	 * Logs out the currently logged in user.
	 */
	public void logout() {
		user = null;
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(WMSApplication.getContext());
		SharedPreferences.Editor editor = settings.edit();
		editor.remove("user_id");
		editor.remove("email");
		editor.remove("phone_num");
		editor.remove("admin");
		editor.remove("locked");
		editor.commit();
	}
	
	/**
	 * Returns whether the session has a logged in user. 
	 * @return whether the session has a logged in user. 
	 */
	public boolean isLoggedIn() {
		return (user != null);
	}
	
	/**
	 * Returns whether the currently logged in user is an administrator.
	 * @return true if the currently logged in user is an administrator, false otherwise.
	 */
	public boolean isAdmin() {
		if (user == null)
			return false;
		return user.isAdmin();			
	}
	
	/**
	 * Returns  the currently logged in user.
	 * @return  the currently logged in user.
	 */
	public User getUser() {
		return user;
	}

	@Override
	public void onDBResultReceived(Object obj) {
		if (obj != null)
			user = (User) obj;
	}

	@Override
	public void onDBRequestFailure(String message) {
		System.out.println(message);
	}


}
